package handlers

import (
	"03/cache"
	"03/models"
	"03/pkg"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
)

func Login(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	var user models.User
	redis_key := "login_" + username
	errData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Int()
	if errData >= 3 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误3次，禁止登录",
		})
		return
	}
	err := models.DB.Where("username=?", username).Debug().First(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "账号或密码错误",
		})
		return
	}
	if user.ID == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "账号或密码错误",
		})
		return
	}
	if pkg.Md5(password) != user.Password {
		cache.Rdb.Incr(cache.Ctx, redis_key)
		if errData == 2 {
			cache.Rdb.Set(cache.Ctx, redis_key, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 500,
				"msg":  "密码错误3次，封禁2小时",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"data": user,
	})
	return
}
func CreateRoleNode(c *gin.Context) {
	rid, _ := strconv.Atoi(c.Query("rid"))
	nid, _ := strconv.Atoi(c.Query("nid"))
	if rid == 0 || nid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.RoleNode{
		Rid: nid,
		Nid: nid,
	}
	models.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "关联成功",
		"data": data,
	})
	return
}
func QueryUserRole(c *gin.Context) {
	/*
		SELECT
		*
		FROM
		roles
		LEFT JOIN user_roles ON roles.id=user_roles.uid
	*/
	uid := c.Query("uid")
	var role models.Role
	models.DB.Table("roles").
		Joins("LEFT JOIN user_roles ON roles.id=user_roles.uid").
		Where("user_roles.uid=?", uid).Debug().First(&role)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": role,
	})
	return
}
func QueryRoleNode(c *gin.Context) {
	/*
		SELECT
		*
		FROM
		nodes
		LEFT JOIN role_nodes ON nodes.id=role_nodes.rid
	*/
	rid := c.Query("rid")
	var node models.Node
	models.DB.Table("nodes").
		Joins("LEFT JOIN role_nodes ON nodes.id=role_nodes.rid").
		Where("role_nodes.rid=?", rid).Debug().First(&node)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": node,
	})
	return
}
func Depot(c *gin.Context) {
	depot_code := c.PostForm("depot_code")
	depot_name := c.PostForm("depot_name")
	if depot_code == "" || depot_name == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.Depot{
		DepotCode: depot_code,
		DepotName: depot_name,
	}
	models.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": data,
	})
	return
}
func DepotOrder(c *gin.Context) {
	did, _ := strconv.Atoi(c.PostForm("did"))
	order_code := c.PostForm("order_code")
	order_name := c.PostForm("order_name")
	order_num := c.PostForm("order_num")
	order_price := c.PostForm("order_price")
	if did == 0 || order_code == "" || order_name == "" || order_num == "" || order_price == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.DepotOrder{
		Did:        did,
		OrderCode:  order_code,
		OrderName:  order_name,
		OrderNum:   order_num,
		OrderPrice: order_price,
	}
	models.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": data,
	})
	return
}
func DepotOrderList(c *gin.Context) {
	/*
		SELECT
		  depot_orders.did OrderCode,
		  depots.depot_name DepotName,
		  depot_orders.order_name OrderName,
		  depot_orders.order_num OrderNum,
		  depot_orders.order_price OrderPrice
		FROM
		depot_orders
		LEFT JOIN depots ON depots.id=depot_orders.did
	*/
	var dol []models.DepotOrderList
	models.DB.Table("depot_orders").
		Select("depot_orders.did OrderCode,\n\t\t  depots.depot_name DepotName,\n\t\t  depot_orders.order_name OrderName,\n\t\t  depot_orders.order_num OrderNum,\n\t\t  depot_orders.order_price OrderPrice").
		Joins("LEFT JOIN depots ON depots.id=depot_orders.did").
		Debug().Find(&dol)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": dol,
	})
	return
}
func DepotOrderDetail(c *gin.Context) {
	/*
		SELECT
		  depot_orders.did OrderCode,
		  depots.depot_name DepotName,
		  depot_orders.order_name OrderName,
		  depot_orders.order_num OrderNum,
		  depot_orders.order_price OrderPrice
		FROM
		depot_orders
		LEFT JOIN depots ON depots.id=depot_orders.did
	*/
	id := c.Query("id")
	var dol models.DepotOrderList
	models.DB.Table("depot_orders").
		Select("depot_orders.did OrderCode,\n\t\t  depots.depot_name DepotName,\n\t\t  depot_orders.order_name OrderName,\n\t\t  depot_orders.order_num OrderNum,\n\t\t  depot_orders.order_price OrderPrice").
		Joins("LEFT JOIN depots ON depots.id=depot_orders.did").
		Where("depot_orders.did=?", id).
		Debug().Find(&dol)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": dol,
	})
	return
}
